package me.monster.grideaprocessor;

import javafx.util.Pair;

import java.io.File;
import java.util.List;

/**
 * @description
 * @author: Created jiangjiwei in 2020/9/28 10:41 下午
 */
public class Entrance {

    public static void main(String[] args) {
        delegateRun(args);
    }

    private static void delegateRun(String[] args) {
        if (!argsValid(args)) {
            printErrorHelp();
            return;
        }
        if (args.length == 1) {
            String arg = args[0];
            if (arg.equals("-h")) {
                printHelp();
            } else if (arg.equals("-v")) {
                printVersion();
            } else {
                Pair<String, String> allPath = pathValid(arg, "");
                if (allPath.getKey() != null && allPath.getKey().length() != 0) {
                    process(allPath.getKey());
                } else {
                    printErrorHelp();
                }
            }
        } else if (args.length == 2) {
            Pair<String, String> allPath = pathValid(args[0], args[1]);
            if (allPath.getKey() != null && allPath.getValue() != null && allPath.getKey().length() != 0 && allPath.getValue().length() != 0) {
                process(allPath.getKey(), allPath.getValue());
            } else {
                printErrorHelp();
            }
        }
    }

    private static void printErrorHelp() {
        System.out.println("参数有误，请使用 -h 查看帮助信息。");
    }

    private static Pair<String, String> pathValid(String inputFolder, String outputFolder) {
        final File inputFile = new File(inputFolder);
        final File outputFile = new File(outputFolder);
        String checkIn = "";
        String checkOut = "";
        if (inputFile.exists() && inputFile.isDirectory()) {
            checkIn = inputFolder;
        }
        if (outputFile.exists() && outputFile.isDirectory()) {
            checkOut = outputFolder;
        }
        return new Pair<>(checkIn, checkOut);
    }

    private static void printVersion() {
        System.out.println("当前版本为：1.0.3.34，查看更多内容请打开：https://gitee.com/jiangjiwei/GrideaProcessor");
    }

    private static void printHelp() {
        System.out.println("-h 查看帮助");
        System.out.println("-v 查看版本号");
        System.out.println("inputFolder 根据输入的路径，自动在同级目录下创建 _processor 后缀的文件夹作为输出路径");
        System.out.println("inputFolder outputFolder 根据 inputFolder 输入原始数据「Gridea 的输出路径」到指定目录下。");
    }

    /**
     * 验证参数是否合法
     *
     * @param args 输入参数
     * @return true 合法，false 不合法，系统将输出提示信息
     */
    private static boolean argsValid(String[] args) {
        if (args == null) {
            return false;
        }
        if (args.length == 0) {
            return false;
        }
        if (args.length > 2) {
            return false;
        }
        return true;
    }

    public static void process(String inputFolder, String outputFolder) {
        long startTime = System.currentTimeMillis();

        if (outputFolder == null || outputFolder.length() == 0) {
            outputFolder = inputFolder + "_processor";
        }
        final List<FileInfo> fileInfos = FileCounter.getInstance(inputFolder).countFile(inputFolder, outputFolder);

        String inFolderName = inputFolder.substring(inputFolder.lastIndexOf("/"));
        for (int i = 0, count = fileInfos.size(); i < count; i++) {
            final FileInfo fileInfo = fileInfos.get(i);
            final float progress = (i + 1) / (float) count;
            System.out.println(String.format("%.2f%% %s", progress * 100, fileInfo.getOutputPath()));
            if (fileInfo.isSkip()) {
                FileUtils.getInstance().saveFile("", fileInfo);
            } else {
                final String html = HtmlParser.getInstance().parse(fileInfo.isInRoot(),fileInfo.getSourcePath(), inFolderName);
                FileUtils.getInstance().saveFile(html, fileInfo);
            }
        }
        long spend = (System.currentTimeMillis() - startTime);
        String spendText = "";
        final float second = (float) spend / 1000;
        if (second < 1) {
            spendText = spend + " 毫秒";
        } else {
            spendText = second + " 秒";
        }
        System.out.println("共处理 " + fileInfos.size() + " 个文件，共计用时：" + spendText);
    }

    public static void process(String inputFolder) {
        String outputFolder = inputFolder + "_processor";
        process(inputFolder, outputFolder);
    }
}
